package com.htrq.entity;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HDept {
	private int id;
	private String text;// 部门名称
	private String ifjq;// 是否是加气站
	private int parentId;// 父ID
	private boolean leaf=true;// 是否是子节点
	private String remark;//
	private List<HDept> children = new ArrayList<HDept>();

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getIfjq() {
		return ifjq;
	}
	public void setIfjq(String ifjq) {
		this.ifjq = ifjq;
	}
	public int getParentId() {
		return parentId;
	}
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
	public boolean isLeaf() {
		return leaf;
	}
	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}
	public List<HDept> getChildren() {
		return children;
	}
	public void setChildren(List<HDept> children) {
		this.children = children;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}
	// 构建ext需要的树形结构的方法
	public static final <T> List<T> buildTree(List<T> nodes) {
		if (null == nodes || nodes.size() == 0)
			return null;
		Map<Integer, T> resources = new HashMap<Integer, T>();
		List<T> result = new ArrayList<T>();
		try {
			for (int i = 0; i < nodes.size(); i++) {
				T node = nodes.get(i);
				Method getId = node.getClass().getMethod("getId");
				Integer id = (Integer) getId.invoke(node);
				resources.put(id, node);
			}
			for (Map.Entry<Integer, T> e : resources.entrySet()) {
				T node = e.getValue();
				Method getparentId = node.getClass().getMethod("getParentId");
				Integer parentId = (Integer) getparentId.invoke(node);
				if (parentId == 0) {
					result.add(node);
				} else {
					T parent = resources.get(parentId);
					if (null != parent) {
						@SuppressWarnings("unchecked")
						List<T> children = (List<T>) parent.getClass().getMethod("getChildren").invoke(parent);
						children.add(node);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
